package com.example.javaweb_project.controller;

import com.example.javaweb_project.model.Employee;
import com.example.javaweb_project.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Timestamp;
import java.time.LocalDate;

@Controller
public class ListController {

    @Autowired
    EmployeeService employeeService;
    @RequestMapping("list")
    public String get_list(ModelMap map, Employee employee){
        map.put("employees",employeeService.list(employee));
        return "list";
    }
    @RequestMapping("list/delete/{id}")
    public String delete(@PathVariable Long id){
        employeeService.deleteById(id);
        return "redirect:/list";
    }
    @RequestMapping("employee/create")
    public String create(){
        return "create";
    }
    @RequestMapping("/employee/save")
    public String save(@RequestParam String age, @RequestParam String name,
                       @RequestParam String address, @RequestParam String email,
                       @RequestParam Integer phone,  @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
                       @RequestParam Integer salary) {
        Employee employee = new Employee(name, Integer.parseInt(age), email, address, phone, date, salary);
        employeeService.save(employee);
        return "redirect:/list";
    }
    @RequestMapping("list/update/{id}")
    public String update (ModelMap map,@PathVariable Long id){

        map.put("employee",employeeService.getbyid(id));//employeeDao.getReferenceById(id));
        return "update";
    }
    @RequestMapping("/list/edit")
    public String edit(Employee st){
        employeeService.save(st);//employeeDao.save(st);
        return "redirect:/list";
    }
}
